package cn.easybuy.dao.product;

import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import cn.easybuy.dao.BaseDao;
import cn.easybuy.entity.ProductCategory;

public class ProductCategoryDaoImpl extends BaseDao implements ProductCategoryDao {

	//	继承BaseDao connection对象
	public ProductCategoryDaoImpl(Connection conn) {
		super(conn);
	}
	
	//	把循环赋值的过程抽取成一个方法
	public ProductCategory tableToClass(ResultSet rs) throws Exception {
		
		ProductCategory pc = new ProductCategory();
		pc.setId(rs.getInt("id"));
		pc.setName(rs.getString("name"));
		pc.setParentId(rs.getInt("parentId"));
		pc.setType(rs.getInt("type"));
		pc.setIconClass(rs.getString("iconClass"));
		return pc;
	}
	
	//	查询所有的商品分类信息
	public List<ProductCategory> queryAllProductCategory(String parentId) {
		
		List<ProductCategory> pcList = new ArrayList<ProductCategory>();
		List<Object> params = new ArrayList<Object>();
		ResultSet rs = null;
		StringBuffer sql = new StringBuffer("SELECT * FROM easybuy_product_category WHERE 1 = 1");
		if (null != parentId && !"".equals(parentId)) {
			sql.append(" and parentId = ?");
			params.add(parentId);
		}
		try {
			rs = this.executeQuery(sql.toString(), params.toArray());
			while (rs.next()) {
				ProductCategory pc = tableToClass(rs);
				pcList.add(pc);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			super.closeAll(null, null, rs);
		}
		
		return pcList;
	}
	
	

}
